using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace whereIsTheDonutGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class DonutSpawner : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SoundEffect soundEffect;
        private float donutRotation;
        //     private float aspectRatio;
        donutGame game;
        public int numberOfDonuts = 15;
        private ICamera camera;
        public List<Donut> donuts { get; set; }

        public DonutSpawner(donutGame game)
            : base(game)
        {
            donuts = new List<Donut>();
            this.game = game;
            donutRotation = 3f;
        }

        public override void Initialize()
        {
            int i;
            Random random = new Random();
            camera = (ICamera)Game.Services.GetService(typeof(ICamera));
            for (i = 0; i < numberOfDonuts; i++)
            {
                donuts.Add(new Donut());
                donuts[i].LoadContent(this.Game.Content);
                int x = (random.Next() % 450);
                if (x < 50) x += 50;
                int z = (random.Next() % 450);
                if (z < 50) z += 50;
                donuts[i].Position = new Vector3(x , game.terrain.Height[x, z] + 5, -z);
            }

            Donut d = new Donut();
            d.LoadContent(this.Game.Content);
            
      
            int x1 = 420;
            int z1 = 255;
            d.Position = new Vector3(x1, game.terrain.Height[x1, z1] + 5, -z1);
            donuts.Add(d);

            Donut e = new Donut();
            e.LoadContent(this.Game.Content);


            x1 = 420;
            z1 = 260;
            e.Position = new Vector3(x1, game.terrain.Height[x1, z1] + 5, -z1);
            donuts.Add(e);
            Donut f = new Donut();
            f.LoadContent(this.Game.Content);


            x1 = 420;
            z1 = 250;
            f.Position = new Vector3(x1, game.terrain.Height[x1, z1] + 5, -z1);
            donuts.Add(f);

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }


        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            foreach (Donut d in donuts)
            {
                if (!d.recieved)
                {
                    foreach (ModelMesh mesh in d.Model.Meshes)
                    {
                        foreach (BasicEffect effect in mesh.Effects)
                        {
                            effect.View = camera.ViewMatrix;
                            effect.Projection = camera.ProjectionMatrix;
                            effect.World = Matrix.CreateRotationY(donutRotation) * Matrix.CreateTranslation(d.Position);
                            effect.EnableDefaultLighting();
                        }
                        mesh.Draw();
                    }
                    d.UpdateBoundingBox();
                    donutRotation += 0.001f;
                    base.Draw(gameTime);
                }

            }
        }
    }
}
